layui.use(['table', 'form', 'layer', 'element', 'jquery'], function () {
    window.jQuery = window.$ = layui.jquery;
    var table = layui.table
        , layer = layui.layer
        , form = layui.form
        ,element = layui.element;
    //方法级渲染
    table.render({
        elem: '#listTable'
        , url: app.api + list.api+"lists"
        , cols: list.columns
        ,cellMinWidth: 60
        , id: 'listTable'
        , page: true
        , height: 'full-180'
        , limits: [10, 15, 20, 30, 50, 100, 500]
        , limit: 10 //默认采用10
        , response: {statusName: 'error', statusCode: 0, msgName: 'message', countName: 'total',dataName: 'rows'}
    });
    
    //监听工具条
    table.on('tool(list)', function(obj){
      var data = obj.data;
      if(obj.event === 'delete'){
        layer.confirm('您确认要删除该数据吗?', function(index){
        	ajaxUpdate(list.api +  "delete", {id:obj.data.id});
        	layer.close(index);
        });
      }else if(obj.event === 'edit'){
    	  window.location = 'edit/id/'+obj.data.id;
      }else if(obj.event === 'view'){
    	  window.location = 'view/id/'+obj.data.id;
      }else{
    	  window.location = obj.event+'/id/'+obj.data.id;
      }
    });


    //批量操作
    $('a.batch').on('click', function () {
        batchUpdate(this);
        event.preventDefault();
    });
    //导出
    $('a.export').on('click', function () {
        window.open(app.api + list.api+"exports?"+$('#searchForm').serialize())
        event.preventDefault();
    });
    
   

    //-----------------------------搜索-----------------------------
    extendForm(form, 'searchForm');

    form.on('submit(searchForm)', function (data) {
        ajaxSearch(data.field);
        return false;
    });
    form.on('submit(inlineForm)', function (data) {
        ajaxSearch(data.field);
        return false;
    });
    //-----------------------------搜索-----------------------------


    /**
     * 批量操作
     * @param ele DOM
     */
    function batchUpdate(ele) {
        var action = $(ele).attr('href');
        if (action.substring(0, 1) == '#') action = action.substring(1);
        var checkStatus = table.checkStatus('listTable');
        if(checkStatus.data.length == 0){
            app.error('请至少选择一条数据!');
            return;
        }
        var ids = '';
        for(var i=0;i<checkStatus.data.length;i++) ids += ',' + checkStatus.data[i].id;
        ids = ids.substring(1);
        var msg = $(ele).attr('alert');
        var param = {id:ids};
        if(msg != ''){
            layer.confirm(msg, function(index){
                ajaxUpdate(list.api + action, param);
                layer.close(index);
            });
        }else{
            ajaxUpdate(list.api + action, param);
        }
    }

    /**
     * 更新操作
     * @param url
     * @param param
     */
    function ajaxUpdate(url, param) {
        app.post(url , param, function () {
            table.reload('listTable');
        });
    }


    /**
     * 搜索
     * @param data 参数
     * @param table layui.table
     */
    function ajaxSearch(data) {
        table.reload('listTable', {
            where: data
        });
    }
});

function format_timestamp(val){
	if(isNaN(val) || val == null || val == 0) return '';
	var date = new Date();
	date.setTime(val * 1000);
	return date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate() + ' '
		+ (date.getHours()+1) + ':' + (date.getMinutes()+1) + ':' + (date.getSeconds()+1);
}

function format_timestamp_date(val){
	if(isNaN(val) || val == null || val == 0) return '';
	var date = new Date();
	date.setTime(val * 1000);
	return date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate();
}

function format_optbar(bars){
    var str = '';
    for(var i=0;i<bars.length;i++){
        if(bars[i] == 'edit'){
            str += '<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">编辑</a>';
        }else if(bars[i] == 'verify'){
            str += '<a class="layui-btn layui-btn-xs" lay-event="verify">审核</a>';
        }else if(bars[i] == 'view'){
            str += '<a class="layui-btn layui-btn-xs" lay-event="view">查看</a>';
        }else if(bars[i] == 'delete'){
            str += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>';
        }
    }
    return str;
}
